package com.geek.gkpush.util.net;

import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;

/**
 * <h1>网络 IP 相关工具类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/9/28 05:58
 */
@Slf4j
public class IpUtils {

    private static String[] IP_ADDR = new String[2];

    static {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            IP_ADDR[0] = inetAddress.getHostAddress();
            IP_ADDR[1] = inetAddress.getHostName();
        } catch (Exception e) {
            log.error("InetAddress.getLocalHost error.", e);
        }
    }

    /**
     * <h2>获取当前机器的 IP 地址。</h2>
     *
     * @param:
     * @return: java.lang.String
     */
    public static String getIp() {
        return IP_ADDR[0];
    }
}